package dima.userinterface;

import dima.vigenere.Crypter;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: dima
 * Date: 09.10.2010
 * Time: 0:55:05
 * To change this template use File | Settings | File Templates.
 */
public class MainForm {
    private JTabbedPane tabbedPane1;
    private JPanel panel1;
    private JButton closeButton;
    private JTextPane openTextPane;
    private JTextPane cipherTextPane;
    private JTextPane keyTextPane;
    private JButton startButton;
    private JRadioButton encodeRadioBtn;
    private JRadioButton decodeRadioBtn;
    private JButton openCipherBtn;
    private JButton openTextBtn;
    private JButton openKeyBtn;
    private JPanel tabVisinger;
    private JPanel tabCrypto;
    private JFrame frame = new JFrame("CryptoLabTwo");

    private static final int FORM_WIDTH = 600;
    private static final int FORM_HEIGHT = 600;
                                                                     

    public MainForm() {

        frame.getContentPane().add(panel1);
        frame.setSize(FORM_WIDTH, FORM_HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (encodeRadioBtn.isSelected()){
                    String cipher = Crypter.crypt(openTextPane.getText(),keyTextPane.getText(),true);
                    cipherTextPane.setText(cipher);
                }else if (decodeRadioBtn.isSelected()){
                    String text = Crypter.crypt(cipherTextPane.getText(),keyTextPane.getText(),false);
                    openTextPane.setText(text);
                }
            }
        });
        openTextBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileOpenDialog = new JFileChooser();
                fileOpenDialog.setDialogTitle("Выберите файл с текстом");
                fileOpenDialog.showOpenDialog(frame);
                FileReader fileIn = null;
                try {
                    fileIn = new FileReader(fileOpenDialog.getSelectedFile());
                    String s = "";
                    openTextPane.read(fileIn,s);
                } catch (FileNotFoundException e1) {

                } catch (IOException e1) {
                    
                }
            }
        });
        openKeyBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileOpenDialog = new JFileChooser();
                fileOpenDialog.setDialogTitle("Выберите файл с текстом");
                fileOpenDialog.showOpenDialog(frame);
                FileReader fileIn = null;
                try {
                    fileIn = new FileReader(fileOpenDialog.getSelectedFile());
                    String s = "";
                    keyTextPane.read(fileIn,s);
                } catch (FileNotFoundException e1) {

                } catch (IOException e1) {

                }
            }
        });
        openCipherBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileOpenDialog = new JFileChooser();
                fileOpenDialog.setDialogTitle("Выберите файл с текстом");
                fileOpenDialog.showOpenDialog(frame);
                FileReader fileIn = null;
                try {
                    fileIn = new FileReader(fileOpenDialog.getSelectedFile());
                    String s = "";
                    cipherTextPane.read(fileIn,s);
                } catch (FileNotFoundException e1) {

                } catch (IOException e1) {

                }
            }
        });
    }

    public JFrame getFrame() {
        return frame;
    }

}
